package com.zkingsoft.services.distribution.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zkingsoft.dao.distribution.DisCouponDao;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.distribution.DisCoupon;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisCouponService;
import com.zkingsoft.util.ModelUtils;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-06-26 07:15
 */
@Service("disCouponService")
public class DisCouponServiceImpl implements DisCouponService {

	@Autowired
	private DisCouponDao disCouponDao;

	@Override
	public int add(DisCoupon disCoupon) {

		return disCouponDao.insert(disCoupon);

	}

	@Override
	public int batchAdd(List<DisCoupon> disCouponList) {
		int num = 0;

		int c = 10000;
		int size = disCouponList.size() / c + 1;
		for (int i = 0; i < size; i++) {
			int begin = i * c;
			int end = (i + 1) * c;
			end = end >= disCouponList.size() ? disCouponList.size() : end;
			List<DisCoupon> insertList = disCouponList.subList(begin, end);
			num += disCouponDao.batchInsert(insertList);
		}
		return num;

	}

	@Override
	public int modifyByMap(DisCoupon oldDisCoupon, DisCoupon newDisCoupon) {

		if (!ModelUtils.isModified(oldDisCoupon, newDisCoupon)) {
			return 1;
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldDisCoupon, newDisCoupon);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("couponId", oldDisCoupon.getCouponId());
			disCouponDao.updateByMap(modifyMap);
		}
		return 1;
	}

	@Override
	public int modifyByModel(DisCoupon disCoupon) {

		return disCouponDao.updateByModel(disCoupon);

	}

	@Override
	public int remove(List<Long> list) {

		return disCouponDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long couponId) {

		return disCouponDao.deleteById(couponId);

	}

	@Override
	public int removeByModel(DisCoupon disCoupon) {

		return disCouponDao.deleteByModel(disCoupon);

	}

	@Override
	public List<DisCoupon> findInPage(DisCoupon disCoupon, PaginationVO pageVo) {

		return disCouponDao.selectInPage(disCoupon, pageVo);

	}

	@Override
	public List<DisCoupon> findByModel(DisCoupon disCoupon) {

		return disCouponDao.selectByModel(disCoupon);

	}

	@Override
	public int findTotal(DisCoupon disCoupon) {

		return disCouponDao.selectTotalRecord(disCoupon);

	}

	@Override
	public DisCoupon findById(Long couponId) {

		return disCouponDao.selectById(couponId);

	}
	
	@Override
	public List<DisCoupon> findMyCouponList(Long desId) {
		
		return disCouponDao.selectMyCouponList(desId);
	}

}